<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Activity Attendance Recording</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" type="text/css" href="./jquery/easyui.css">
	<link rel="stylesheet" type="text/css" href="./jquery/icon.css">
	<style>
		.easyui-dialog{
		    height:485px!important;
		}
		.panel-window{
		    height:450px!important;
		}
		.easyui-validatebox{
			width:175px!important;
		}
	</style>
</head>
<?php
	define('CONFIG_FILE',true);
	include './config.php';
	define('DB_FILE',true);

	session_start();
	if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
		Header("Location: ./Index.php");
		exit();
	}else{
		$userType = $_SESSION["userType"];
	}

	// set timeout period in seconds
	$inactive = 7200;
	// check to see if $_SESSION['timeout'] is set
	if (isset($_SESSION["timeout"])) {
		$session_life = time() - $_SESSION["timeout"];
		if ($session_life > $inactive) {
			session_destroy();
			Header("Location: Timeout.php");
		}
	}
	$_SESSION["timeout"] = time();
	
?>
<body onLoad="document.nricform.barcode.focus();">
    <div class="banner">
	</div>
	<div id="main">
		<div class="left-wrapper">
			<div class="logo">
				<img src="images/eldercare.gif" alt="logo" width="240px" />
			</div>
			<div class="menu-wrapper">
				<div id='cssmenu' >    
						<?php
							if ($userType == "Therapy Assistant") {
								include_once('./Sidebar/_SidebarTA.php');
							} else if ($userType == "Centre Manager") {
								include_once('./Sidebar/_SidebarCM.php');
							} else {
								include_once('./Sidebar/_SidebarSA.php');
							}
						?>
				</div>				
			</div>
			<div class="clear"></div>
		</div>
		<div id="content">
			<div class="content2">	
				<b style="float:right; margin: 0px 0 0 0">  
					<?php
						if (!empty($_SESSION["centreName"])) {
							$Username = $_SESSION["username"];
							$centreName = $_SESSION["centreName"];
							echo "Welcome, " . $Username . " ! " . $centreName . " Branch";
						} else {
							$Username = $_SESSION["username"];
							echo "Welcome, " . $Username;
						}
					?>
					<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
				</b>
				</p>
                <br>
				<div style="float:center">
				<br>
				<h3> Activity Attendance Recording </h3>
				<br>
				<table id="dg" title="Clients" class="easyui-datagrid" style="width:auto;height:430px; "
					   url="./ActivityAttendance/get_actAttendance.php"
					   toolbar="#toolbar" pagination="true"
					   rownumbers="true" fitColumns="true" singleSelect="true">
					<thead>
						<tr>
							<th field="NRIC" width="50" sortable="true">NRIC</th>
							<th field="CustomerName" width="80" sortable="true">Name</th>
							<th field="Gender" width="28" sortable="true">Gender</th>
							<th field="Age" width="28" sortable="true">Age</th>
							<th field="Daycare" width="28" sortable="true">Daycare</th>
							<th field="SignInTimeStamp" width="70" sortable="true">SignInTimeStamp</th>
							<th field="CentreName" width="70" sortable="true">Centre Name</th>
							<th field="ActivityList" width="130">Activity Participated</th>
						</tr>
					</thead>
				</table>
				<div id="toolbar">
					<a href='#' class='easyui-linkbutton' iconCls='icon-add' plain='true' onclick='openAddDlg()'>Add Activity Attendance Record</a>
					<input id="searchid" name="searchid" type="text" style="border-radius:25px">
					<a href="#" class="easyui-linkbutton" iconCls="icon-search" onClick="getClient()">Search</a>
					<a href="#" class="easyui-linkbutton" onClick="findAll()">All</a>
				</div>

				<!-- Insert Dialog and Form -->
				<div>
					<div id="insertdlg" class="easyui-dialog" style="width:400px;height:380px;padding:10px 20px"
						 closed="true" buttons="#insertdlg-buttons">
						<div class="ftitle">New Activity Record</div>
						<form id="insertform" method="post" novalidate>
							<div class="fitem">
								<label>NRIC: </label>
								<input id="NRIC" name="NRIC" class="easyui-validatebox" required="true"  readonly = "true" style="width:155" height='auto'>
							</div>
							<div class="fitem">
								<label>Name: </label>
								<input id="CustomerName" name="CustomerName" class="easyui-validatebox" required="true"  readonly = "true" style="width:155" height='auto'>
							</div>
							<div class="fitem">
								<label>Gender: </label>
								<input id="Gender" name="Gender" class="easyui-validatebox" required="true"  readonly = "true" style="width:155" height='auto'>
							</div>
							<div class="fitem">
								<label>Age: </label>
								<input id="Age" name="Age" class="easyui-validatebox" required="true"  readonly = "true" style="width:155" height='auto'>
							</div>
							<div class="fitem">
								<label>Daycare: </label>
								<input id="Daycare" name="Daycare" class="easyui-validatebox" required="true"  readonly = "true" style="width:155" height='auto'>
							</div>
							<div class="fitem">
								<label>Sign In Time Stamp: </label>
								<input id="SignInTimeStamp" name="SignInTimeStamp" class="easyui-validatebox" required="true"  readonly = "true" style="width:175" height='auto'>
							</div>
							<div class="fitem">
								<label>Centre Name: </label>
								<input id="CentreName" name="CentreName" class="easyui-validatebox" required="true" readonly = "true" style="width:175" height='auto' >
							</div>
							<div class="fitem">
								<label>Activity Centre: </label>
								<select id="activityCentre" name="activityCentre" required ="true" style="width:155" height='auto'>
									<?php
										require_once './Tool/DB/SqlDBManager.class.php';
										$sqlDBManager = new SqlDBManager();
										if($_SESSION["userType"]=="System Admin"){
											$sql="SELECT CentreId, CentreName FROM centre";  
											$res=$sqlDBManager->queryRows($sql); 
										}
										else{
											$sql="SELECT CentreId, CentreName FROM centre where CentreName=?";
											$parameters=array($_SESSION["centreName"]);
											$res=$sqlDBManager->queryRows($sql,$parameters);
										}		
										if(!empty($res)){
											for($i=0;$i<count($res);$i++){
												echo "<option value=\"".$res[$i]['CentreId']."\">".$res[$i]['CentreName']."</option>";			
											}
										}
										//close connection
   										$sqlDBManager->close_connect();
									?>
								</select>							
							</div>
							<div class="fitem">
								<label>Activity Name: </label>
								<select id="activityId" name="activityId" required ="true" onmouseover="showActivity()" style="width:155" height='auto'>
									<option value="0" selected disabled>--None--</option>
								</select>							
							</div>
							<div class="fitem">
								<label>Performance: </label>
								<select id="performance" name="performance"required ="true" style="width:155" height='auto'>
									<option value="1" selected>1=Poor</option>
									<option value="2">2=Average</option>
									<option value="3">3=Good</option>
								</select>							
							</div>
							<div class="fitem">
								<label>Remarks: </label>
								<input id="remarks" name="remarks" class="easyui-validatebox" style="width:155" height='auto' >
							</div>
						</form>
					</div>
					<div id="insertdlg-buttons">
						<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="addActRecord()">Add</a>
						<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="insertCancel()">Cancel</a>
					</div>
				</div>

				<div class="clear"></div>
				</div>
			</div>
		</div>
	</div>
</body>
<div id="footer">&copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
<script type="text/javascript" src="./jquery/jquery-1.6.min.js"></script>
<script type="text/javascript" src="./jquery/jquery.min.js"></script>
<script type="text/javascript" src="./jquery/jquery.easyui.min.js"></script>
<script type='text/javascript'>
	function openAddDlg(){
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			$('#insertdlg').dialog('open').dialog('setTitle', 'Add Activity Record');
			$('#insertform').form('load', row);
		}
	}

	function addActRecord(){
		$('#insertform').form('submit',{
			onSubmit: function(){
				$(this).form('validate');
				//validate input	
				var flagNRIC=0;
				var flagActivityCentre=0;
				var flagActivityId=0;
				var flagPerformance=0;
				var flagRemarks=0;
				$.ajax({
					async : false,
					type : "POST",
					url : "./ActivityAttendance/validateInsert.php", //the page/method you wanna validate
					data : {
						NRIC: $("#NRIC").val(),
						ActivityCentre: $("#activityCentre").val(),
						ActivityId:$("#activityId").val(),
						Performance:$("#performance").val(),
						Remarks:$("#remarks").val()
					},
					success : function(data) {
						var data = JSON.parse(data);
						flagNRIC=data.flagNRIC;
						flagActivityCentre=data.flagActivityCentre;
						flagActivityId=data.flagActivityId;
						flagPerformance=data.flagPerformance;
						flagRemarks=data.flagRemarks;
					}
				});
				if(flagNRIC==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Singapore NRIC Format.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagActivityCentre==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Centre Id. Centre Id cannot contain any letters and special characters.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagActivityId==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Activity Id. Activity Id cannot contain any special characters and letters.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagPerformance==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Performance Type. Performance Type cannot contain any letters and special characters.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagRemarks==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Remarks. Remarks cannot contain any special characters.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else{
					return $(this).form('validate');	
				}
			},
			url: './ActivityAttendance/save_actAttendance.php',
			success: function(result) {
				var result = eval('('+ result +')');
				if (result.success) {
					$('#insertform').find('input[name="remarks"]').val('');
					$('#insertdlg').dialog('close');// close the dialog
					$.messager.show({
						title: 'Success',
						msg: 'Activity record has taken for the elderly.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});		
					$('#dg').datagrid('reload');	// reload the user data
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg,
						showType:'fade',
						style:{
							right:'',
							bottom:''
						}
					});
				}
			}
		});
	}

	function getClient() {
		//validate search input    
        var check = 0;
        $.ajax({
            async : false,
            type : "POST",
            url : "./ActivityAttendance/validateSearch.php", //the page/method you wanna validate
            data: "searchid=" + $("#searchid").val(),
            success : function(data) {
                check=data;
            }
        });
        if(check==0){
            $.messager.show({
                title: 'Error',
                msg: 'Invalid Search Input. Search Input cannot contain any special characters except bracket, dashes, colons and single quatation marks.',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
            });
        }else{
        	$('#dg').datagrid('load', {
				searchid: $('#searchid').val()
			});	
        }
	}
	function findAll() {
		$('#dg').datagrid('load', {
		});
	}

	function insertCancel(){
		$('#insertform').find('input[name="remarks"]').val('');
		$('#insertdlg').dialog('close');
	}

	function showActivity(){
		var xmlhttp;
		if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(xmlhttp){
			var centreName = $('[name="activityCentre"]').val();
			xmlhttp.open("GET","./ActivityAttendance/dropdownList.php?q="+centreName,true);
			xmlhttp.onreadystatechange=function(){
				if(this.readyState ==4 && xmlhttp.status==200){
					var data = JSON.parse(xmlhttp.responseText);
					$('#activityId')[0].options.length = 1;
					for(var i=0;i<data.length;i++) 
					{	
						var opt = document.createElement('option');				
						opt.value=data[i].activityId;
						opt.innerHTML = data[i].activityName;
						document.getElementById("activityId").appendChild(opt);
					}
				}
			}
			xmlhttp.send();
		}	
	}	
</script>
</html>